#!/bin/bash -xe

REPODIR=$(dirname $(realpath $0))/../../..
INSTALLDIR=$(realpath -m $(dirname $0)/../tmp/mono)

error()
{
    echo "$(date "+%F@%T") :: $1" 1>&2
    exit 1
}

report()
{
    echo "$(date "+%F@%T") :: $1"
}

download()
{
	wget --quiet -O ${REPODIR}/pkgconfig.zip http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/pkg-config_0.26-1_win32.zip
}

clean()
{
	rm -rf ${INSTALLDIR}
}

build()
{
	pushd ${REPODIR}
	report "Building Mono"
	mkdir -p ${INSTALLDIR}
	EXTRA_CONF_FLAGS="--prefix=${INSTALLDIR}" CI_TAGS="win-i386,no-tests" scripts/ci/run-jenkins.sh || error "*** Could not run autogen ***"
	popd
}

install()
{
	pushd ${REPODIR}
	MONO_EXECUTABLE=${REPODIR}/msvc/build/sgen/Win32/bin/Release/mono-sgen.exe make install || error "*** Installing to temporary prefix failed ***"
	rm -f	${INSTALLDIR}/bin/mono-sgen.* \
		${INSTALLDIR}/bin/mono-boehm.* \
		${INSTALLDIR}/lib/libmono-2.0.* \
		${INSTALLDIR}/bin/libmonoboehm-2.0.* \
		${INSTALLDIR}/lib/libmonoboehm-2.0.* \
		${INSTALLDIR}/bin/libmonosgen-2.0.* \
		${INSTALLDIR}/lib/libmonosgen-2.0.* \
		${INSTALLDIR}/bin/*MonoPosixHelper.* \
		${INSTALLDIR}/lib/*MonoPosixHelper.* \
		${INSTALLDIR}/bin/mono \
		${INSTALLDIR}/bin/pedump \
		${INSTALLDIR}/bin/mono-package-runtime \
		${INSTALLDIR}/bin/monodis*
	pushd ${REPODIR}
	cp -a msvc/build/sgen/Win32/bin/Release/monodis-sgen.exe	${INSTALLDIR}/bin/monodis.exe
	cp -a msvc/build/sgen/Win32/bin/Release/monodis-sgen.pdb	${INSTALLDIR}/bin/monodis.pdb
	cp -a msvc/build/sgen/Win32/bin/Release/mono-sgen.exe	${INSTALLDIR}/bin/
	cp -a msvc/build/sgen/Win32/bin/Release/mono-sgen.pdb	${INSTALLDIR}/bin/
	cp -a msvc/build/sgen/Win32/bin/Release/mono-2.0-sgen.dll	${INSTALLDIR}/bin/
	cp -a msvc/build/sgen/Win32/bin/Release/mono-2.0-sgen.pdb	${INSTALLDIR}/bin/
	cp -a msvc/build/sgen/Win32/lib/Release/mono-2.0-sgen.lib	${INSTALLDIR}/lib/
	cp -a msvc/build/sgen/Win32/bin/Release/mono-2.0-sgen.pdb	${INSTALLDIR}/lib/
	cp -a msvc/build/sgen/Win32/lib/Release/libmono-static-sgen.lib	${INSTALLDIR}/lib/
	cp -a msvc/build/sgen/Win32/bin/Release/MonoPosixHelper.dll	${INSTALLDIR}/bin/
	cp -a msvc/build/sgen/Win32/bin/Release/MonoPosixHelper.pdb	${INSTALLDIR}/bin/
	cp -a msvc/build/sgen/Win32/lib/Release/MonoPosixHelper.lib	${INSTALLDIR}/lib/
	cp -a msvc/build/sgen/Win32/bin/Release/MonoPosixHelper.pdb	${INSTALLDIR}/lib/
	cp -a msvc/build/boehm/Win32/bin/Release/mono-boehm.exe		${INSTALLDIR}/bin/
	cp -a msvc/build/boehm/Win32/bin/Release/mono-boehm.pdb		${INSTALLDIR}/bin/
	cp -a msvc/build/boehm/Win32/bin/Release/mono-2.0-boehm.dll	${INSTALLDIR}/bin/
	cp -a msvc/build/boehm/Win32/bin/Release/mono-2.0-boehm.pdb	${INSTALLDIR}/bin/
	cp -a msvc/build/boehm/Win32/lib/Release/mono-2.0-boehm.lib	${INSTALLDIR}/lib/
	cp -a msvc/build/boehm/Win32/bin/Release/mono-2.0-boehm.pdb	${INSTALLDIR}/lib/
	if [ -e msvc/build/sgen/Win32/bin/Release/libmono-btls-shared.dll ]; then
		cp -a msvc/build/sgen/Win32/bin/Release/libmono-btls-shared.dll	${INSTALLDIR}/bin/
	fi
	cp -a msvc/build/boehm/Win32/lib/Release/libmono-static-boehm.lib	${INSTALLDIR}/lib/
	cp -a ${INSTALLDIR}/bin/mono-sgen.exe	${INSTALLDIR}/bin/mono.exe
	cp -a ${INSTALLDIR}/bin/mono-sgen.pdb	${INSTALLDIR}/bin/mono.pdb
	popd
	pushd ${INSTALLDIR}/bin
	for i in *
		do if [[ "`file -b $i`" == "POSIX shell script, ASCII text"* ]]
			then RUNTIME=`grep -ho -m 1 "xbuild/.*/bin/\|[2,3,4]\.[0,5]/" $i | sed 's#/$##'`
			if [ -n "$(grep -ho '.:/.*\.exe' $i)" ]
				then EXENAME=$(basename `grep -ho '.:/.*\.exe' $i`)
			fi
			if [ -f "../lib/mono/${RUNTIME}/${EXENAME}" ]
				then sed "s#@@RUNTIME@@#${RUNTIME}#g" ${REPODIR}/packaging/Windows/resources/wrapper.bat > $i.bat
				sed -i "s#@@COMMAND@@#${EXENAME}#g" $i.bat
				sed -i 's#/#\\#g' $i.bat
			fi
			sed -i "s#.:/.*/#\"\$\(cygpath -w \"\$\{BASH_SOURCE%/\*\}\"/../lib/mono/${RUNTIME}/\)\"#g" $i
			sed -i 's#/.*/tmp/mono/bin#\"\$\{BASH_SOURCE%/\*\}\"#g' $i
		fi
	done
	unzip -jd . ${REPODIR}/pkgconfig.zip bin/pkg-config.exe
	chmod a+x *.bat *.exe
	popd
	popd

	find ${INSTALLDIR} -name monodoc.dll.config -exec sed -i 's#value=".*/lib/monodoc#value="C:\\Program Files (x86)\\Mono\\lib\\monodoc#g' {} \;
}

prerequisites()
{
	which mcs 2>/dev/null || error "*** mcs is not in \$PATH ***"
}

$@
